package com.kafka_demo.kafka.listener;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.kafka.support.ProducerListener;
import org.springframework.stereotype.Component;

/**
 * 生产者回调监听
 *
 * @author : z_sz
 * @date : 2020-09-03 15:37
 **/
@Component
public class KafkaProducerListener implements ProducerListener<Object, Object> {

    /**
     * 成功回调
     *
     * @param producerRecord 生产记录
     * @param recordMetadata 记录数据
     */
    @Override
    public void onSuccess(ProducerRecord<Object, Object> producerRecord, RecordMetadata recordMetadata) {
        System.out.println("推送成功，推送数据：" + producerRecord.value().toString());
    }

    /**
     * 失败回调
     *
     * @param producerRecord 生产记录
     * @param exception      异常信息
     */
    @Override
    public void onError(ProducerRecord<Object, Object> producerRecord, Exception exception) {
        System.out.println("推送失败，推送数据：" + producerRecord.value().toString() + "，失败原因：" + exception.getMessage());
    }
}
